/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.dao;

import br.com.business.FilmeBO;
import br.com.business.TipoMidiaBO;
import br.com.dao.util.GenericDao;
import br.com.model.Item;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import br.com.type.CodigoBarras;
import br.com.type.EstadoItem;
import br.com.model.TipoMidia;
import br.com.model.Filme;
import br.com.servlets.CadastroItem;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;




/**
 *
 * @author Hercules Sandim
 */
public class ItemDao implements GenericDao<Item> 
    {

    private Connection connection;

    public ItemDao(Connection connection) {
        this.connection = connection;
    }
    
    @Override
    public void save(Item item) {

        try
        {

            String sql;
            
            //PreparedStatement stmt = this.connection.prepareStatement(sql);


       
                    
            sql="insert into item " +
                    " (numeroserie,dtaquisicao, estado,tipomidia,filme) "+
                    " values (?,?,?,?,?)"  ;
            System.out.println(sql);
            PreparedStatement stmt = this.connection.prepareStatement(sql);
            stmt.setString(1, item.getNumeroSerie().getCodigoBarras().toString());
            stmt.setString(2, item.getDtAquisicao().toString());            
            stmt.setString(3, item.getEstado().toString());            
            stmt.setString(4, item.getTipoMidia().getIdTipoMidia().toString());
            stmt.setString(5, item.getFilme().getIdFilme().toString());   
            stmt.executeUpdate();
        }    
        catch (SQLException e)
        {
                e.printStackTrace();
        }
        
    }

    @Override
    public void delete(Item item) {
       try
        { 
            String id, sql;
            id = item.getNumeroSerie().toString();
            sql="delete from item where numeroserie =  "+id;
            PreparedStatement stmt = this.connection.prepareStatement(sql); 
            stmt.executeUpdate();
        }    
        catch (SQLException e)
        {
                e.printStackTrace();
        }
        
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public LinkedList<Item> list() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Item findById(Integer id) {
        Item item = null;
        try{
            String sql = "select * from item where numeroserie = '" + id + "'";
            PreparedStatement stmt = this.connection.prepareStatement(sql); 
            ResultSet rs = stmt.executeQuery();
            if(rs.next()){
                 CodigoBarras codigo = null; 
                try {
                    codigo = new CodigoBarras(rs.getString(1));
                } catch (Exception ex) {
                    Logger.getLogger(CadastroItem.class.getName()).log(Level.SEVERE, null, ex);
                }

                TipoMidia midia = new TipoMidia(rs.getInt(4));
                Filme filme = new Filme(rs.getInt(5));                
                item  = new Item( codigo  , rs.getDate(2), EstadoItem.valueOf(rs.getString(3)), midia , filme);
           
            }     
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return item;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public LinkedList<Item> listar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
      public LinkedList<Item> listarDisponivel() {

        LinkedList<Item> lista = new LinkedList<Item>();
        String sql = "select numeroserie, dtaquisicao, estado, tipomidia, filme FROM item where estado <> 'RESERVADO'";
        PreparedStatement stmt;
        Item item;

        try {
            stmt = this.connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Filme fm = (new FilmeBO().getFilmeById(rs.getInt(5)));
                TipoMidia tm = (new TipoMidiaBO()).getTipoMidiaById(rs.getInt(4));
                CodigoBarras cb = null;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                try {
                    cb = new CodigoBarras(rs.getString(1));
                } catch (Exception ex) {
                    Logger.getLogger(ItemDao.class.getName()).log(Level.SEVERE, null,
                            ex);
                }

                Date dtAquisicao = null;
                try {
                    dtAquisicao = new Date(sdf.parse(rs.getString(2)).getTime());
                } catch (ParseException ex) {
                    Logger.getLogger(CadastroItem.class.getName()).log(Level.SEVERE,
                            null, ex);
                }
                item = new Item(cb, dtAquisicao, EstadoItem.valueOf(rs.getString(3)), tm, fm);
                lista.add(item);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ItemDao.class.getName()).log(Level.SEVERE,
                    null, ex);
        }

        return lista;
    }

    @Override
    public Item getObjectById(Integer id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void updateReservado(Item it) {
        System.out.println("entrou");
        try {
            
            String sql = "update item set estado = 'RESERVADO' where numeroserie = "+it.getNumeroSerie().getCodigoBarras().toString();
            PreparedStatement stmt = this.connection.prepareStatement(sql);
            stmt.executeUpdate();
        } catch (Exception e) {
        }
    } 

    public LinkedList<Item> getItemDoFilme(Integer idFilme){
        
        LinkedList<Item> lista = new LinkedList<Item>();
        String sql = "select numeroserie, dtaquisicao, estado, tipomidia, filme FROM item where filme = " + idFilme;
        PreparedStatement stmt;
        Item item;

        try {
            stmt = this.connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Filme fm = (new FilmeBO().getFilmeById(rs.getInt(5)));
                TipoMidia tm = (new TipoMidiaBO()).getTipoMidiaById(rs.getInt(4));
                CodigoBarras cb = null;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                try {
                    cb = new CodigoBarras(rs.getString(1));
                } catch (Exception ex) {
                    Logger.getLogger(ItemDao.class.getName()).log(Level.SEVERE, null,
                            ex);
                }

                Date dtAquisicao = null;
                try {
                    dtAquisicao = new Date(sdf.parse(rs.getString(2)).getTime());
                } catch (ParseException ex) {
                    Logger.getLogger(CadastroItem.class.getName()).log(Level.SEVERE,
                            null, ex);
                }
                item = new Item(cb, dtAquisicao, EstadoItem.valueOf(rs.getString(3)), tm, fm);
                lista.add(item);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ItemDao.class.getName()).log(Level.SEVERE,
                    null, ex);
        }

        return lista;        
    }
    
    public Item getObjectByNumeroSerie(String numeroSerie){
        
        Item item = null;
        
        try{    
            String sql = "select * from item where numeroserie = '" + numeroSerie + "'";
            PreparedStatement stmt = this.connection.prepareStatement(sql); 
            ResultSet rs = stmt.executeQuery();
            if(rs.next()){
                 CodigoBarras codigo = null; 
                try {
                    codigo = new CodigoBarras(rs.getString(1));
                } catch (Exception ex) {
                    Logger.getLogger(CadastroItem.class.getName()).log(Level.SEVERE, null, ex);
                }
                TipoMidia midia = new TipoMidiaBO().getTipoMidiaById(rs.getInt(4));
                Filme filme = new FilmeBO().getFilmeById(rs.getInt(5));
                item  = new Item( codigo  , rs.getDate(2), EstadoItem.valueOf(rs.getString(3)), midia , filme);
            }     
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return item;        
    }
    
    @Override
    public LinkedList<Item> getObjectByName(String name) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}